<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/Sys/css/layui.css">
    <link rel="stylesheet" href="/Sys/css/sign.css">


    <link rel="stylesheet"
          href="/Sys/js/css/modules/layui-icon-extend/iconfont.css">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/Sys/css/font.css">
    <link rel="stylesheet" href="/Sys/css/xadmin.css">


    <script type="text/javascript"
            src="/Sys/js/jquery-3.3.1.min.js"></script>
    <script src="/Sys/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <script type="text/javascript" src="/Sys/js/jquery.table2excel.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">


    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <div class="demoTable" style="margin-bottom: 20px">
            <div class="layui-inline">
                <input class="layui-input" name="searchKey" id="demoReload" placeholder="输入姓名或用户名搜索"
                       autocomplete="off">
            </div>
            <button class="layui-btn" data-type="reload">
                <i class="layui-icon">&#xe615;</i>搜索
            </button>
        </div>
    </div>

    <xblock>
        <button class="layui-btn layui-btn-danger" onclick="delAll()">
            <i class="layui-icon">&#xe640;</i>批量删除
        </button>
        <button class="layui-btn" onclick="stuAdd()">
            <i class="layui-icon">&#xe654;</i>添加学生
        </button>
    </xblock>
    <table id="test" lay-filter="test"></table>

</div>
</body>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">
        <i class="layui-icon">&#xe642;</i>编辑
    </a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
        <i class="layui-icon">&#xe640;</i>删除
    </a>
</script>


<script>
    var documentWidth = $(document).width();
    layui.use('table', function () {
        var table = layui.table;
        table.render({
            elem: '#test'  //绑定table表格
            , id: 'stuInfo'
            , method: 'post'
            , url: 'getUserList' //后台springmvc接收路径
            , where: {
                role: 'STUDENT'
            }
            , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                , groups: 1 //只显示 1 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页
                , limit: 5
                , limits: [5, 10, 15]
            }
            , cols: [
                [
                    {type: 'checkbox', width: documentWidth * 4 / 100}
                    , {field: 'id', title: 'ID', width: documentWidth * 8 / 100, sort: true}
                    , {field: 'username', title: '用户名', width: documentWidth * 10 / 100}
                    , {field: 'name', title: '姓名', width: documentWidth * 8 / 100}
                    , {field: 'phone', title: '手机号', width: documentWidth * 10 / 100}
                    , {field: 'email', title: '邮箱', width: documentWidth * 12 / 100}
                    , {field: 'photo', title: '用户头像', width: documentWidth * 10 / 100, templet: function(d){
                        return d.photo == null ?
                            '<img src="/Sys/images/defaultUser.png" style="width:30px;height:30px;border-radius:50%;">':
                            '<img src="/Sys/uploads/'+d.photo+'" style="width:30px;height:30px;border-radius:50%;">';
                    }}
                    ,
                    {
                        field: 'operation',
                        title: '操作',
                        toolbar: '#barDemo',
                        width: documentWidth * 15 / 100,
                        fixed: 'right'
                    }
                ]
            ]
        });

        //监听工具条
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { //编辑
                layer.open({
                    type: 2,
                    title: '修改学生信息',
                    skin: 'layui-layer-molv',
                    shadeClose: false,
                    shade: 0.8,
                    area: ['700px', '600px'],
                    content: 'stuModi?id=' + data.id,
                    end: function () {
                        obj.update();
                        location.reload();
                    }
                });
            } else if (layEvent === 'del') { //删除
                layer.confirm('确认要删除该学生吗？', function (index) {
                    $.ajax({
                        url: 'deleteUser',
                        data: {'id': data.id},
                        type: 'post',
                        success: function (res) {
                            if (res === "success") {
                                layer.msg('删除成功', {icon: 1, time: 2000, shade: 0.2});
                                obj.del();
                            } else {
                                layer.msg('删除失败', {icon: 2, time: 3000, shade: 0.2});
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });

        var $ = layui.$, active = {
            reload: function () {
                var demoReload = $('#demoReload');
                table.reload('stuInfo', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    url: 'getStuById'
                    , where: {
                        'key[id]': demoReload.val(),
                        'role': 'STUDENT'
                    }
                    , method: 'post'
                });
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });


    function delAll(argument) {
        layui.use('table', function () {
            var table = layui.table
            var checkStatus = table.checkStatus('stuInfo');
            if (checkStatus.data.length == 0) {
                parent.layer.msg('请先选择要删除的数据行！', {icon: 2});
                return;
            }
            var ids = "";
            for (var i = 0; i < checkStatus.data.length; i++) {
                ids += checkStatus.data[i].id + ",";
            }

            parent.layer.msg('删除中...', {icon: 16, shade: 0.3, time: 5000});

            $.ajax({
                url: 'deleteUsers',
                data: {'nums': ids},
                type: 'post',
                success: function (data) {
                    if (data == "success") {
                        parent.layer.msg('删除成功！', {icon: 1, time: 2000, shade: 0.2});
                        location.reload(true);
                    } else {
                        parent.layer.msg('删除失败！', {icon: 2, time: 3000, shade: 0.2});
                    }
                }
            });
        });

    }

    function deleteUser() {
        layui.use('table', function () {
            var table = layui.table
            var checkStatus = table.checkStatus('stuInfo');
            if (checkStatus.data.length == 0) {
                parent.layer.msg('请先选择要删除的数据行！', {icon: 2});
                return;
            }
            var id = checkStatus.data[0].id;
            layer.confirm('确认要删除该学生吗？', function (index) {
                $.ajax({
                    url: 'deleteUser',
                    data: {'id': id},
                    type: 'post',
                    success: function (data) {
                        if (data == "success") {
                            parent.layer.msg('删除成功！', {icon: 1, time: 2000, shade: 0.2});
                            location.reload(true);
                        } else {
                            parent.layer.msg('删除失败！', {icon: 2, time: 3000, shade: 0.2});
                        }
                    }
                });
                layer.close(index);
            });
        });
    }

    function stuAdd() {
        layer.open({
            type: 2,
            title: '添加学生',
            skin: 'layui-layer-lan',
            shadeClose: false,
            shade: 0.8,
            area: ['700px', '600px'],
            resize: true,
            content: 'stuAdd',
            end: function () {
                window.location.reload();
            }
        });
    }

    function stuModi() {
        layui.use('table', function () {
            var table = layui.table;
            var checkStatus = table.checkStatus('stuInfo');
            if (checkStatus.data.length === 0) {
                layer.msg('请先选择要修改的行', {icon: 2});
                return;
            }
            var id = checkStatus.data[0].id;
            layer.open({
                type: 2,
                title: '修改学生信息',
                skin: 'layui-layer-molv',
                shadeClose: false,
                shade: 0.8,
                area: ['700px', '600px'],
                content: 'stuModi?id=' + id,
                end: function () {
                    window.location.reload();
                }
            });
        });
    }


    function screen() {
        //获取当前窗口的宽度
        var width = $(window).width();
        if (width > 1200) {
            return 3;   //大屏幕
        } else if (width > 992) {
            return 2;   //中屏幕
        } else if (width > 768) {
            return 1;   //小屏幕
        } else {
            return 0;   //超小屏幕
        }
    }


    function exportTable() {
        location.href = "/Sys/export";
    }


    layui.use(['form', 'layer', 'table', 'upload'], function () {
        var table = layui.table
            , form = layui.form, upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#uploadExcel'
            , exts: "xls"
            , url: '/Sys/ImportStu'
        });

    })


</script>

</html>